//
//  TLAlertView.m
//  FunnyVideo
//
//  Created by 李鑫 on 15/9/30.
//  Copyright © 2015年 NewEggTech. All rights reserved.
//
#define TLAlertBackImgNoneCmt @"NoneCmt"
#define TLAlertBackImgNetBlock @"Back_Alert_NetBlock"
#define TLAlertBackImgLoading @"Back_Alert_Loading"

#import "TLAlertView.h"

@interface TLAlertView ()

@property (nonatomic,strong)TLAlertTapBlock tapBlock;
@property (nonatomic,strong) UIImageView *backImgView;
@property (nonatomic,strong) UITapGestureRecognizer *tapRecognizer;
@end

@implementation TLAlertView
- (id)initWithFrame:(CGRect)frame alertImage:(UIImage *)backImg{
    self = [super initWithFrame:frame];
    
    if(self)
    {
        self.backImgView = [[UIImageView alloc] initWithFrame:[self caculateCenterRectAtFrame:frame]];
        
        [self.backImgView setImage:backImg];
        
        [self.backImgView setContentMode:UIViewContentModeCenter];
        
        
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewBackgroundTap:)];
        
        self.tapRecognizer = tapRecognizer;
        
        //[self addSubview:self.backImgView];
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame alertImage:(UIImage *)backImg refreshBlock:(TLAlertTapBlock)block{
    //assgin block
    self.tapBlock = block;
    
    return [self initWithFrame:frame alertImage:backImg];
}

- (void)showAlert{
    dispatch_async(dispatch_get_main_queue(), ^{
    [self addSubview:self.backImgView];    
    });
}

- (void)showAlert:(TLAlertType)alertType{
    dispatch_async(dispatch_get_main_queue(), ^{
        self.backImgView.backgroundColor = [UIColor blackColor];
        self.backImgView.image = [UIImage imageNamed:[self imageNameByAlertType:alertType]];
    });
}

- (void)dismissAlert{
    dispatch_async(dispatch_get_main_queue(), ^{
    [self.backImgView removeFromSuperview];
    });
}


#pragma mark -- Action
- (void)viewBackgroundTap:(UIGestureRecognizer *)gs{
    if((gs.state == UIGestureRecognizerStateChanged) && (gs.numberOfTouches == 1))
    {
        self.tapBlock(YES);
    }
    else
    {
        self.tapBlock(NO);
    }
}
#pragma mark -- Frame Caculate
- (CGRect)caculateCenterRectAtFrame:(CGRect)tFrame
{
    CGSize ctSize = CGSizeMake(tFrame.size.width, tFrame.size.height/2);
    CGRect ctFrame = CGRectMake((tFrame.size.width - ctSize.width)/2, (tFrame.size.height - ctSize.height)/2, ctSize.width, ctSize.height);
    
    return ctFrame;
}

#pragma mark -- TLAlertType Trans Image Name
- (NSString *)imageNameByAlertType:(TLAlertType)alertType{
    switch (alertType) {
        case TLAlertTypeNetBlock:
            return TLAlertBackImgNetBlock;
            break;
        case TLAlertTypeNoneData:
            return TLAlertBackImgNoneCmt;
            break;
        case TLAlertTypeSucc:
            return TLAlertBackImgLoading;
            break;
            
            default:
            return TLAlertBackImgLoading;
            break;
    }
}
@end
